package com.scala.zhenghui

import scala.collection.mutable.ArrayBuffer
import scala.util.Random

object Array_ArrayBufferDemo {

  def main(args: Array[String]) {
   ArrayDemo()
    // ArrayBufferDemo()
    //ArrayBufferErgodic()
    ArrayBufferOperate()
  }


  def ArrayDemo(): Unit = {
    println()
    val a = new Array[Int](10)
    var num = a.length
    for (i <- 0 until a.length)
      a(i) = Random.nextInt(100)
    for (i <- 0 until a.length)
      print(a(i) + ",")
    println()

    scala.util.Sorting.quickSort(a);
    for (i <- 0 until a.length)
      print(a(i) + ",")

  }

  def ArrayBufferDemo(): Unit = {
    println()
    var b = ArrayBuffer[Int]()
    //1
    b += 1
    for (i <- 0 until b.length)
      print(b(i) + ",")
    //2
    println()

    b +=(2, 3, 4, 5)
    println("+=(2,3,4,5)后")
    for (i <- 0 until b.length)
      print(b(i) + ",")
    //3
    println()
    b ++= Array(6, 7, 8, 9, 10)
    println("++= Array(6, 7, 8, 9, 10)后：")
    for (i <- 0 until b.length)
      print(b(i) + ",")
    //4
    println()
    println("b.trimEnd(5)后：")
    b.trimEnd(5)
    for (i <- 0 until b.length)
      print(b(i) + ",")
    //5
    println()
    println("b.insert(4,3)后：")
    b.insert(5, 3)
    for (i <- 0 until b.length)
      print(b(i) + ",")
    //6
    println()
    println("b.insert(5,1,2,3,4,5)后：")
    b.insert(5, 1, 2, 3, 4, 5)
    for (i <- 0 until b.length)
      print(b(i) + ",")

    //7
    println()
    println("b.remove(10)后：")
    b.remove(10)
    for (i <- 0 until b.length)
      print(b(i) + ",")


    //8
    println()
    println("b.remove(8,2)后：")
    b.remove(8, 2)
    for (i <- 0 until b.length)
      print(b(i) + ",")
    //8
    println()
    println("bArray：")
    var bArray = b.toArray
    for (i <- 0 until bArray.length)
      print(bArray(i) + ",")

  }


  def ArrayBufferErgodic(): Unit = {
    println()
    val a = ArrayBuffer[String]()
    a +=("Hello", "World", "Scala", "Java", "Hadoop", "Hive")
    //1
    for (i <- a)
      print(i + ",")
    //2
    println()
    for (i <- 0 until a.length)
      print(a(i) + ",")
    //2
    println()
    for (i <- 0 until(a.length, 2))
      print(a(i) + ",")

    //3
    println()
    for (i <- 0 until a.length if i % 2 == 1)
      print(a(i) + ",")
    //4
    println()
    for (i <- (0 until a.length).reverse)
      print(a(i) + ",")
    //5
    println()
    for (i <-( 0 until(a.length, 2)).reverse)
      print(a(i) + ",")
  }

  def ArrayBufferOperate(): Unit ={
    println()
    var a = ArrayBuffer[Int]()
    //1
    println()
    a +=(1,2, 3, 4, 5)

    println(a.toString())
    println(a.mkString)
    println(a.mkString(","))
    println(a.sum)

  }
}

